/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.outHouse.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.suli.outHouse.domain.OrderDetail;
import me.zhengjie.suli.outHouse.service.OrderDetailService;
import me.zhengjie.suli.outHouse.service.dto.OrderDetailQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @website https://eladmin.vip
* @author wyh
* @date 2024-02-01
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "出库详细管理")
@RequestMapping("/api/orderDetail")
public class OrderDetailController {

    private final OrderDetailService orderDetailService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('orderDetail:list')")
    public void exportOrderDetail(HttpServletResponse response, OrderDetailQueryCriteria criteria) throws IOException {
        orderDetailService.download(orderDetailService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询出库详细")
    @ApiOperation("查询出库详细")
    @PreAuthorize("@el.check('orderDetail:list')")
    public ResponseEntity<Object> queryOrderDetail(OrderDetailQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(orderDetailService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增出库详细")
    @ApiOperation("新增出库详细")
    @PreAuthorize("@el.check('orderDetail:add')")
    public ResponseEntity<Object> createOrderDetail(@Validated @RequestBody OrderDetail resources){
        return new ResponseEntity<>(orderDetailService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改出库详细")
    @ApiOperation("修改出库详细")
    @PreAuthorize("@el.check('orderDetail:edit')")
    public ResponseEntity<Object> updateOrderDetail(@Validated @RequestBody OrderDetail resources){
        orderDetailService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除出库详细")
    @ApiOperation("删除出库详细")
    @PreAuthorize("@el.check('orderDetail:del')")
    public ResponseEntity<Object> deleteOrderDetail(@RequestBody String[] ids) {
        orderDetailService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/findDetailByMainId/{id}")
    @Log("/根据出库单id查询详细")
    public ResponseEntity<Object> findDetailByMainId(@PathVariable("id") String id){
        return orderDetailService.findDetailByMainId(id);
    }
}